package com.yh.correct.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yh.common.utils.codec.MD5Utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yh.correct.model.ResultData;
import com.yh.correct.model.SysRole;
import com.yh.correct.model.SysUser;
import com.yh.correct.model.SysUserRole;
import com.yh.correct.service.IJudicialDeptmentService;
import com.yh.correct.service.ISysRoleService;
import com.yh.correct.service.ISysUserService;

import javax.servlet.http.HttpSession;

/**
 * 用户管理控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sysuser")
public class SysUserController {

	//日志
	private static Logger logger = LogManager.getLogger(SysUserController.class);
	
	//用户服务层接口
	@Autowired
	private ISysUserService userService;
	
	//所属单位服务层接口
	@Autowired
	private IJudicialDeptmentService deptService;
	
	//角色服务层接口
	@Autowired
	private ISysRoleService roleService;
			
	/**
	 * 跳转到用户管理页面
	 * @return
	 */
	@RequestMapping(value="/show")
	public String toUserPage(){
		return "/system/sysuser";
	}


	/*
	* 跳转用户回显，更改密码页面
	* */
	@RequestMapping(value="/update")
	public String toUpdUser(Model model, HttpSession session){
		SysUser user=(SysUser)session.getAttribute("userIdLogin");
		String passWord=user.getPassword();
		String salt=user.getSalt();


		Map<String,Object> userObj=null;
		try {
			userObj=userService.getSysUser(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		userObj.put("password",passWord);


		model.addAttribute("userObject", userObj);
		return "/system/update";
	}


	/*
	* 更改密码，md5加密
	* */
	@RequestMapping("updPassword")
	@ResponseBody
    public ResultData updPass(String passWord,HttpSession session){
		int i=0;
		SysUser user=(SysUser)session.getAttribute("userIdLogin");
		user.setPassword(passWord);
		try {
			i=userService.updateSysUserPwd(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setMessage("插入数据成功！");
		}else{
			rd.setCode(4000);
			rd.setMessage("插入数据失败！");
		}
		return rd;
	}

	
	/**
	 * 动态模糊查询用户列表
	 * @param username
	 * @param dept
	 * @param status
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/search")
	@ResponseBody
	@RequiresPermissions("user:list")
	public ResultData searchUser(String username,String dept,String status,int pageNum,int pageSize){
		Map<String,Object> condition=new HashMap<String,Object>();
		condition.put("username", username);
		condition.put("dept", dept);
		condition.put("status", status);
		condition.put("pageNum", pageNum);
		condition.put("pageSize", pageSize);
		PageInfo<Object> pageInfo=null;
		try {
			pageInfo=userService.findListByCondition(condition);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		List<Object> pageList=new ArrayList<Object>();
		pageList.add(pageInfo);

		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(pageList);
		return rd;
	}
	
	/**
	 * 跳转到用户增加页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toAdd")
	@RequiresPermissions("user:add")
	public String toAddUser(Model model){
		List<Object> depts=null;
		List<SysRole> roles=null;
		try {
			depts=deptService.listJudicialDeptments();
			roles=roleService.findAllSysRole();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("depts", depts);
		model.addAttribute("roles", roles);
		return "/system/sysuser_add";
		
	}
	
	
	/**
	 * 增加用户操作
	 * @param user
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value="/doAdd")
	@ResponseBody
	public ResultData doAddUser(SysUser user,Integer roleid){
		int i=0;
		try {
			i=userService.insertSysUser(user, roleid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setMessage("插入数据成功！");
		}else{
			rd.setCode(4000);
			rd.setMessage("插入数据失败！");
		}
		return rd;
	}
	
	/**
	 * 跳转到修改用户页面
	 * @param user
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toEdit")
	public String toEditUser(SysUser user,Model model){
		Map<String,Object> userObject=null;
		List<Object> depts=null;
		List<SysRole> roles=null;
		try {
			userObject=userService.getSysUser(user);
			depts=deptService.listJudicialDeptments();
			roles=roleService.findAllSysRole();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("depts", depts);
		model.addAttribute("roles", roles);
		model.addAttribute("userObject", userObject);
		return "/system/sysuser_edit";
	}
	
	/**
	 * 更新用户操作
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/doEdit")
	@ResponseBody
	public ResultData doEditUser(SysUser user,SysUserRole userrole){
		int i=0;
		try {
			i=userService.updateSysUser(user,userrole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setMessage("更新数据成功！");
		}else{
			rd.setCode(4000);
			rd.setMessage("更新数据失败！");
		}
		return rd;
	}
	
	/**
	 * 删除用户操作
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/doDel")
	@ResponseBody
	public ResultData doDelUser(SysUser user){
		int i=0;
		try {
			i=userService.deleteSysUser(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setMessage("删除数据成功！");
		}else{
			rd.setCode(4000);
			rd.setMessage("删除数据失败！");
		}
		return rd;
	}
	
	/**
	 * 显示用户详细信息
	 * @param user
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/detail")
	public String detailUser(SysUser user,Model model){
		Map<String,Object> userObj=null;
		try {
			userObj=userService.getSysUser(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("userObject", userObj);
		return "/system/sysuser_detail";
	}
	
	
	/**
	 * 修改用户密码
	 * @param id
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/doEditPwd")
	@ResponseBody
	public ResultData doEditPwd(SysUser user){
		int i=0;
		try {
			i=userService.updateSysUserPwd(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd=new ResultData();
		if(i>0){
			rd.setMessage("修改密码成功！");
		}else{
			rd.setCode(4000);
			rd.setMessage("修改密码失败！");
		}
		return rd;
	}

	/**
	 * 查询所有用户
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value="/getAllUser")
	@ResponseBody
	public List<SysUser> getAllUser() throws Exception {
		return userService.getAllUser();
	}
}
